package com.bdf.ipharma.renderers;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

import com.bdf.ipharma.config.Session;
import com.bdf.ipharma.gui.GUIButtonImage;
import com.bdf.ipharma.gui.GUIPopUpCounter;
import com.bdf.ipharma.gui.GuiExtraction2;
import com.bdf.ipharma.gui.Images;
import com.bdf.ipharma.model.map.ProductoStockMap;

public class JTableButtonRenderer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer   {

	JPanel panel;
	
	JLabel text;
	GUIButtonImage showButton;
	
	private GuiExtraction2 guiExtraction2;
	private Session session;
	private GUIPopUpCounter popUp;
	private ProductoStockMap producto;
	
	
	public JTableButtonRenderer(GuiExtraction2 guiExtraction2) {
		super();
		this.guiExtraction2 = guiExtraction2;
		this.session = this.guiExtraction2.session;
		text = new JLabel();
		showButton = new GUIButtonImage("", Images.butt1, Images.butt1Dn);
		showButton.setForeground(Color.white);
		showButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(JTableButtonRenderer.this.guiExtraction2.isTableEnabled){
					if(popUp!=null){
						popUp.setVisible(true);
						popUp.setProducto(JTableButtonRenderer.this.producto);
					}
				}
				/*
				 * JOptionPane.showMessageDialog(
				 * JOptionPane.getFrameForComponent(button),
				 * "Button clicked for row " + rowIndex);
				 */
				
			}
		});
		
		
		panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		panel.add(text);
		panel.add(showButton);
	}

	
	private void updateData(ProductoStockMap producto, boolean isSelected, JTable table) {
		this.producto=producto;
		popUp = new GUIPopUpCounter(JTableButtonRenderer.this.session,JTableButtonRenderer.this.guiExtraction2);
		popUp.setModalityType( Dialog.ModalityType.APPLICATION_MODAL);
		popUp.setProducto(producto);
		text.setText("");
		if (isSelected) {
			panel.setBackground(Color.lightGray);
		}else{
			panel.setBackground(Color.lightGray);
		}		
	}
	
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return null;
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		ProductoStockMap producto = (ProductoStockMap)value;
		updateData(producto, true, table);
		return panel;
	}

	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		ProductoStockMap producto = (ProductoStockMap)value;
		updateData(producto, isSelected, table);
		return panel;
	}		
	

}
